home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / libs / cwrlib31.lha / Testlib.c < prev   
C/C++ Source or Header  |  1992-07-22  |  4KB  |  143 lines

  1. #include <exec/types.h>
  2. #include <exec/libraries.h>
  3. #include <libraries/dos.h>
  4. #include <intuition/intuition.h>
  5. #include <graphics/gfxbase.h>
  6. #include <proto/all.h>
  7. #include <string.h>
  8.  
  9. int CXBRK(void) { return(0); }        /* Disable Lattice CTRL/C handling */
  10. int chkabort(void) { return(0); }    /* really */
  11.  
  12. #include "cwritelibbase.h"
  13. #include "cwritelib.h"
  14.  
  15. void main(int,char**);
  16. struct IntuitionBase *IntuitionBase;
  17. struct GfxBase *GfxBase;
  18. struct CwrlibBase *CwrlibBase;
  19. struct CwrData *cdata;
  20.  
  21. UBYTE screentitle[] = "Test program for cwrite.library";
  22. struct Window *TheWindow;
  23.  
  24. UBYTE cursortype = CURSORTYPE_FAST;
  25.  
  26. static void cleanup(char *string)
  27. {
  28.     int returncode = RETURN_OK;
  29.  
  30. /* Write string, if it is there, and set the returncode to ERROR */
  31.     if(string)
  32.     {
  33.         Write(Output(),"Error: ",7);
  34.         Write(Output(),string,strlen(string));
  35.         returncode = RETURN_FAIL;
  36.     }
  37. /* Close everything down.. */
  38.     if(TheWindow)    CWriteCloseWindow(TheWindow);
  39.     if(cdata)    CWriteFree(cdata);
  40.     if(CwrlibBase)    CloseLibrary((struct Library*)CwrlibBase);
  41. /* and exit to CLI or WorkBench */
  42.     Exit(returncode);
  43. }
  44.  
  45. static int Showfile(char *filename)
  46. {
  47.     register int i;
  48.     UBYTE out[256];
  49.     BPTR fd;
  50.  
  51. /* Are we able to open the file?    */
  52.     if(fd = Open(filename,MODE_OLDFILE))
  53.     {
  54. /* Yes, so we will first clear the screen with a "<ESC>c" command */
  55.         CWrite(cdata,"c",2);
  56.         do
  57.         {
  58. /* Write the same amount of chars we receive from Read() to CWrite() */
  59.             if(i = Read(fd,(char*)&out,255))
  60.                 CWrite(cdata,out,i);
  61.         } while(i == 255);
  62. /* No more characters. Close file, and return that file was read ok. */
  63.         Close(fd);
  64.         return(TRUE);
  65.     }
  66. /* File could not be opened, so we return a FALSE */
  67.     else
  68.         return(FALSE);
  69. }
  70.  
  71. void main(int argc, char **argv)
  72. {
  73.     int height = 200;
  74.  
  75.     if(argc==0)    /* Started from Workbench! */
  76.         cleanup(NULL);
  77.     if(argc < 2)
  78.         cleanup("\nUsage: Testlib Filename <cursortype>\n");
  79.  
  80. /* Open cwrite.library */
  81.     if(!(CwrlibBase=(struct CwrlibBase *)OpenLibrary("cwrite.library",0)))
  82.         cleanup("Can't open cwrite.library\n");
  83.  
  84. /* Use the pointers in CwrlibBase for gfx and intuition */
  85.     GfxBase = (struct GfxBase*)CwrlibBase->GfxLib;
  86.     IntuitionBase = (struct IntuitionBase*)CwrlibBase->IntLib;
  87.  
  88. /* If this should be a PAL system, we'll change the window height to 256: */
  89.     if(GfxBase->DisplayFlags & PAL)
  90.         height = 256;
  91.  
  92. /* Allocate memory for our CwrData structure */
  93.     cdata = CWriteAlloc();
  94.     if(!cdata)
  95.         cleanup("Can't allocate memory for CWrite functions!\n\7");
  96.  
  97. /* Open a screen and window for output with CWrite() */
  98.     TheWindow = CWriteOpenWindow(screentitle,640,height,4,0);
  99. /* The screen may be optained from the window pointer, like this: */
  100. /*    TheScreen = TheWindow->WScreen;*/
  101.     if(!TheWindow)
  102.         cleanup("Can't open the window or screen!\n\7");
  103.  
  104. /* We don't need to use CWriteSetColors() here, as the library automatically
  105.    sets the colors upon opening the window and screen.
  106.    This is just an example on how you should call the routine, if you should
  107.    decide to open the window and screen by yourself. */
  108. /*    CWriteSetColors(TheWindow);*/
  109.  
  110. /* Initialize our CwrData structure for use with the CWrite() function. */
  111.     CWriteInit(cdata,TheWindow);
  112.  
  113. /* If there are 2 args, the second argument is the cursortype.. */
  114.     if(argc == 3)
  115.     {
  116.         switch(argv[2][0])
  117.         {
  118.             case '0':    /* None    */
  119.                 cursortype = CURSORTYPE_NONE;
  120.                 break;
  121.             case '1':    /* Fast    */
  122.                 cursortype = CURSORTYPE_FAST;
  123.                 break;
  124.             case '2':    /* Slow    */
  125.                 cursortype = CURSORTYPE_SLOW;
  126.                 break;
  127.             default:
  128.                 cursortype = CURSORTYPE_FAST;
  129.                 break;
  130.         }
  131.     }
  132.     CWriteCursorType(cdata,cursortype);
  133.  
  134. /* Show the filename that was written as the first argument. */
  135.     if(!Showfile(argv[1]))
  136.         cleanup("Unable to open file!\n\7");
  137.     else
  138.         Delay(100);    /* Wait for 2 seconds..    */
  139.  
  140. /* Clean up everything, and exit properly. */
  141.     cleanup(NULL);
  142. }
  143.